package com.iris.live.services.services.impl;

import com.iris.live.services.data.models.RoleItemModel;
import com.iris.live.services.data.repositories.RoleItemRepository;
import com.iris.live.services.services.RoleItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yitianle on 2017/8/10.
 */
@Service
@Transactional
public class RoleItemServiceImpl implements RoleItemService{

    private Logger logger = LoggerFactory.getLogger(RoleItemService.class);

    @Autowired
    private RoleItemRepository roleItemRepository;

    @Override

    public void batch(List<RoleItemModel> list) {
        List<Integer> roleIdList = new ArrayList<Integer>();
        for (RoleItemModel model:list) {
            if(!roleIdList.contains(model.getItemId())){
                roleIdList.add(model.getItemId());
            }
        }
        for (int id:roleIdList) {
            roleItemRepository.deleteRoleItemByPermitAndItemId(list.get(0).getPermit(),id);
        }
        roleItemRepository.save(list);
    }

    @Override
    @Transactional(readOnly = true)
    public List<RoleItemModel> isSetByPermitAndItemId(String permit, Integer itemId) {
        return roleItemRepository.isSetByPermitAndItemId(permit,itemId);
    }

    @Override
    public int deleteRoleItemByPermitAndItemId(String permit, Integer itemId) {
        return roleItemRepository.deleteRoleItemByPermitAndItemId(permit,itemId);
    }

    @Override
    public int deleteRoleItemByPermit(String permit) {
        return roleItemRepository.deleteRoleItemByPermit(permit);
    }
}
